package com.bw.module_live.fans.model.repository

import com.bw.module_live.api.LiveApi
import com.bw.module_live.fans.model.entity.SendGiftRequestEntity
import com.google.gson.Gson
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.toRequestBody
import javax.inject.Inject

/**
 * @author ytx
 * @date 2025-10-27 15:19
 * @description
 */
class LiveRepository @Inject constructor(val api: LiveApi) {

     fun enterLiveRoom(roomId:String)  = flow {
        emit(api.enterLiveRoom(roomId))
    }.flowOn(Dispatchers.IO)

     fun exitLiveRoom(roomId:String)  = flow {
        emit(api.exitLiveRoom(roomId))
    }.flowOn(Dispatchers.IO)

    fun joinRedPacket(roomId: String) = flow {
        val map = mutableMapOf<String,String>()
        map["room_id"] = roomId
        val requestBody = Gson().toJson(map).toRequestBody("application/json;charset=utf-8".toMediaType())
        emit(api.joinRedPacket(requestBody))
    }.flowOn(Dispatchers.IO)

    fun goodsList(roomId:String) = flow {
        emit(api.goodsList(roomId))
    }.flowOn(Dispatchers.IO)

    fun addHotLiveRoom(roomId:String) = flow {
        emit(api.addHotLiveRoom(roomId))
    }.flowOn(Dispatchers.IO)

    fun sendRoomGift(entity: SendGiftRequestEntity) = flow {
        val requestBody = Gson().toJson(entity).toRequestBody("application/json; charset=utf-8".toMediaType())
        emit(api.sendRoomGift(requestBody))
    }.flowOn(Dispatchers.IO)


}